package com.wgchao.authmanager.domain.model;

import com.wgchao.authmanager.infrastructure.exception.ObjectDisabledException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: UserRole
 * @author: wgchao
 * @createTime: 2020/4/26 5:07 PM
 * @Description: 账户角色信息
 */
@Getter
public class UserRole extends DomainObject {
    private String uname;
    private List<RoleIdentity> roleIds;
    private Operator creator;

    public UserRole(String uname, List<RoleIdentity> roleIds, Operator creator) {
        this.uname = uname;
        this.roleIds = roleIds;
        this.creator = creator;
    }

    public UserRole(String uname, Operator creator) {
        this.uname = uname;
        this.creator = creator;
    }

    public void addRole(Role role){
        if(!role.normal()){
            throw new ObjectDisabledException(role.getRoleName()+"角色不可用");
        }
        if(this.roleIds == null){
            this.roleIds = new ArrayList<>();
        }
        this.roleIds.add(role.getIdentity());
    }
}
